<?php
/**
 * Copyright (c) 2013 pipapai.com
 */
JLoader::register('PResumesModelResumes', OC::$SERVERROOT."/apps/resumes/lib/resumes.php");
JLoader::register('PResumesModelSearch', OC::$SERVERROOT."/apps/resumes/lib/search.php");
JLoader::register('PResumesHelper', OC::$SERVERROOT."/apps/resumes/lib/helper.php");
JLoader::register("PCompanyHelper", OC::$SERVERROOT."/apps/company/lib/helper.php");
JLoader::register("PCalendarModellistview", OC::$SERVERROOT."/apps/calendar/lib/listview.php");
JLoader::register("PCalendarHelper", OC::$SERVERROOT."/apps/calendar/lib/helper.php");
JLoader::register('ProvinceHelper', JPATH_SITE.'/components/com_city/helper.php');

class PResumesController {

   // Folder Center
   public static function folder() {
      if(PFactory::isJobseekerUser()) {
         OC_Response::redirect(OC_Helper::linkToRoute("jobseeker_home"));
         return;
      }

      //get uri by foldername
      $dir = JRequest::getString('dir', null);
      $foldername = JRequest::getString('foldername', null);
      if($dir == null && $foldername !== null) {
         $model = new PResumesModelResumes(); 
         $resumefolder = $model->getFolderIdByName($foldername);
         if($resumefolder) {
            $dir = $resumefolder->folder_id;
            $foldername = $resumefolder->summary;
            // if foldername is true, it's auto-created subfolder, do redirect so we can customize client folder name 
            if($foldername) {
               OC_Response::redirect(OC_Helper::linkToRoute('resumes_folder')."?dir=".$dir."&foldername=".JFilterOutput::stringURLUnicodeSlug($foldername));
               return;
            }
         }
      }

      $app = PFactory::getApplication();
      $resumedir = $app->getUserState('resumedir', null);
      if($resumedir) {
         unset($resumedir['showsearch']);
         unset($resumedir['porderby']);
         unset($resumedir['porderdir']);
         $app->setUserState('resumedir', $resumedir);
      }
      
      self::show();
   }
   
   public static function jobpos() {
      $app = PFactory::getApplication();
      // $resumedir = $app->getUserState('resumedir', null);
      // if ($resumedir) {
      //    $resumedir['showsearch'] = 0;
      // } else {
      //    $resumedir = array('showsearch' => 0);
      // }
      // $app->setUserState('resumedir', $resumedir);

      $searchUrl = PResumesHelper::getSearchUrl();
      list($searchUrl, $selected) = PResumesHelper::getSearchItemHref($searchUrl, 'jobpos', '1');
      
      $app->redirect($searchUrl);
      return;
   }

   // get the list. if the jobid is given, only show resumes belong to that jobid 
   public static function show() {
      if(!PUtil::checkUserLogin(true))
         return; 
      
      if(PFactory::isJobseekerUser()) {
         OC_Response::redirect(OC_Helper::linkToRoute("jobseeker_home"));
         return;
      }
      
      $jobid = JRequest::getInt('jobid', 0);

      
      $app = PFactory::getApplication();
      $resumedir = $app->getUserState('resumedir', null);
      
      // get search url
      $searchUrl = PResumesHelper::getSearchUrl();
      
      $advancedSearch = null;
      $jobpos = JRequest::getInt('jobpos', 0);
      $showsearch = JArrayHelper::getValue($resumedir, 'showsearch', 1);
      if ($showsearch) {
         $advsearchTmp = new OCP\Template('resumes', 'part.resume.advancedsearch', '');
         $advsearchTmp->assign('jobpos', $jobpos);
         $advsearchTmp->assign('searchurl', $searchUrl);
         $advancedSearch = $advsearchTmp->fetchPage();
      }
         
      if(PCompanyHelper::isJobManager() && 
         ((!$jobid && !PCompanyHelper::isJobManagerViewOnly()) || ($jobid && PCompanyHelper::checkJobManagerPermissions($jobid)))) {
         $permissions = OCP\PERMISSION_READ;
         $permissions |= OCP\PERMISSION_CREATE;
         $permissions |= OCP\PERMISSION_UPDATE;
         $permissions |= OCP\PERMISSION_DELETE;
         $permissions |= OCP\PERMISSION_SHARE;
      }
      else {
         // default to read permission 
         $permissions = OCP\PERMISSION_READ;
         $permissions |= OCP\PERMISSION_SHARE;
      }
      
      $model = new PResumesModelResumes(); 
      
      $folderinfo = '';
      $dir = JRequest::getString('dir', null);
      $folderinfo = $model->getFolderNameById($dir);
      list($resumes, $pagination, $token, $jobid, $jobtitle) = $model->getList();

      // make breadcrumb und filelist markup
      $list = new OCP\Template('resumes', 'part.list', '');
      $list->assign('resumes', $resumes);
      $list->assign('showdetail', JArrayHelper::getValue($resumedir, 'showdetail', 0));
      $list->assign('baseURL', OCP\Util::linkToRoute('resumes_index'));
      $list->assign('viewURL', OCP\Util::linkToRoute('resume_view', array('id' => '')));
      $list->assign('downloadURL', OCP\Util::linkToRoute('resume_download', array('resume' => '/')));
      $list->assign('disableSharing', false);
      $list->assign('searchtoken', $token);
      
      $resumeStatuses = PResumesHelper::getResumeStatuses();
      $jobList = PResumesHelper::getJobList(PFactory::getMasterUser()->id, 11,'','','',false,false);
      $tagList = PResumesHelper::getTagList(PFactory::getMasterUser()->id, 11);
      
      $tmpl = new OCP\Template('resumes', 'index', 'user');
      $tmpl->assign('baseURL', OCP\Util::linkToRoute('resumes_index'));
      $tmpl->assign('page_title', '云简历');
      $tmpl->assign('resumeList', $list->fetchPage());
      $tmpl->assign('pagination', $pagination);
      $tmpl->assign('jobpos', $jobpos);
      $tmpl->assign('jobList', $jobList);
      $tmpl->assign('tagList', $tagList);
      $tmpl->assign('position', $jobtitle);
      $tmpl->assign('permissions', $permissions);
      $tmpl->assign('resumeStatuses', $resumeStatuses);
      $tmpl->assign('resumes', $resumes);
      $tmpl->assign('allowZipDownload', intval(OCP\Config::getSystemValue('allowZipDownload', true)));
      $tmpl->assign('searchUrl', $searchUrl);
      $tmpl->assign('advancedSearch', $advancedSearch);
      $tmpl->assign('showdetail', JArrayHelper::getValue($resumedir, 'showdetail', 0));
      $tmpl->assign('orderBy', JArrayHelper::getValue($resumedir, 'orderby', 'added'));
      $tmpl->assign('orderDir', strtolower(JArrayHelper::getValue($resumedir, 'orderdir', 'desc')));
      $tmpl->assign('topnlist', JRequest::getInt('topnlist', 0));
      $tmpl->assign('searchtoken', $token);
      $tmpl->assign('folderinfo',$folderinfo);
      $tmpl->printPage();
   }

   public static function topsearch() {
      if(!PUtil::checkUserLogin(true))
         return; 

      if(PFactory::isJobseekerUser()) {
         OC_Response::redirect(OC_Helper::linkToRoute("jobseeker_home"));
         return;
      }
      
      if(PCompanyHelper::isJobManager()) {
         $permissions = OCP\PERMISSION_READ;
         $permissions |= OCP\PERMISSION_CREATE;
         $permissions |= OCP\PERMISSION_UPDATE;
         $permissions |= OCP\PERMISSION_DELETE;
         $permissions |= OCP\PERMISSION_SHARE;
      }
      else {
         // default to read permission 
         $permissions = OCP\PERMISSION_READ;
      }

      $model = new PResumesModelSearch(); 
      list($resumes, $pagination, $jobid, $jobtitle) = $model->topSearch();


      $app = PFactory::getApplication();
      $resumedir = $app->getUserState('resumedir', null);
      if(!$resumedir) {
         $resumedir = array();
      }
      $resumedir['showsearch'] = 0;
      $app->setUserState('resumedir', $resumedir);
      
      // get search url
      $searchUrl = PResumesHelper::getSearchUrl();
      list($searchUrl, $selected) = PResumesHelper::getSearchItemHref($searchUrl, 'jobpos', '1');
      
      $advancedSearch = null;
      $jobpos = JRequest::getInt('jobpos', 1);
      $showsearch = JArrayHelper::getValue($resumedir, 'showsearch', 0);
      if ($showsearch) {
         $advsearchTmp = new OCP\Template('resumes', 'part.resume.advancedsearch', '');
         $advsearchTmp->assign('jobpos', $jobpos);
         $advsearchTmp->assign('searchurl', $searchUrl);
         $advancedSearch = $advsearchTmp->fetchPage();
      }

      // make breadcrumb und filelist markup
      $list = new OCP\Template('resumes', 'part.list', '');
      $list->assign('resumes', $resumes);
      $list->assign('showdetail', JArrayHelper::getValue($resumedir, 'showdetail', 0));
      $list->assign('baseURL', OCP\Util::linkToRoute('resumes_index'));
      $list->assign('viewURL', OCP\Util::linkToRoute('resume_view', array('id' => '')));
      $list->assign('downloadURL', OCP\Util::linkToRoute('resume_download', array('resume' => '/')));
      $list->assign('disableSharing', false);

      $resumeStatuses = PResumesHelper::getResumeStatuses();
      $jobList = PResumesHelper::getJobList(PFactory::getMasterUser()->id, 11);
      $tagList = PResumesHelper::getTagList(PFactory::getMasterUser()->id, 11);

      $tmpl = new OCP\Template('resumes', 'index', 'user');
      $tmpl->assign('baseURL', OCP\Util::linkToRoute('resumes_index'));
      $tmpl->assign('page_title', '云简历');
      $tmpl->assign('resumeStatuses', $resumeStatuses);
      $tmpl->assign('resumeList', $list->fetchPage());
      $tmpl->assign('pagination', $pagination);
      $tmpl->assign('jobpos', $jobpos);
      $tmpl->assign('jobList', $jobList);
      $tmpl->assign('tagList', $tagList);
      $tmpl->assign('position', $jobtitle);
      $tmpl->assign('permissions', $permissions);
      $tmpl->assign('resumes', $resumes);
      $tmpl->assign('allowZipDownload', intval(OCP\Config::getSystemValue('allowZipDownload', true)));
      $tmpl->assign('searchUrl', $searchUrl);
      $tmpl->assign('advancedSearch', $advancedSearch);
      $tmpl->assign('showdetail', JArrayHelper::getValue($resumedir, 'showdetail', 0));
      $tmpl->assign('orderBy', JArrayHelper::getValue($resumedir, 'orderby', 'added'));
      $tmpl->assign('orderDir', strtolower(JArrayHelper::getValue($resumedir, 'orderdir', 'desc')));
      $tmpl->assign('topnlist', JRequest::getInt('topnlist', 0));
      $tmpl->printPage();

   }

   public static function toplist() {
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      
      if(PFactory::isJobseekerUser()) {
         OC_Response::redirect(OC_Helper::linkToRoute("jobseeker_home"));
         return;
      }
      
      $model = new PResumesModelSearch();
      list($resumes, $pagination, $jobid, $jobtitle) = $model->topList();

      $tmpl = new OCP\Template('resumes', 'part.resume.toplist', '');
      $tmpl->assign('resumes', $resumes);
      $tmpl->assign('baseURL', OCP\Util::linkToRoute('resumes_index'));
      $tmpl->assign('viewURL', OCP\Util::linkToRoute('resume_view', array('id' => '')));
      $tmpl->assign('showmoreURL', OCP\Util::linkToRoute('resumes_index') . '?jobid='.$jobid.'&title='.JFilterOutput::stringURLUnicodeSlug($jobtitle));
      $tmpl->printPage();
   }

   public static function search() {
      if(PFactory::isJobseekerUser()) {
         OC_Response::redirect(OC_Helper::linkToRoute("jobseeker_home"));
         return;
      }
      $app = PFactory::getApplication();
      $resumedir = $app->getUserState('resumedir', null);
      if($resumedir) {
         unset($resumedir['porderby']);
         unset($resumedir['porderdir']);
         $app->setUserState('resumedir', $resumedir);
      }
      
      self::show();
   }
   
   public static function getjoblist() {
      $companyid = 0;
      if(!OC_User::isLoggedIn()) {
         $app = PFactory::getApplication();
         $companyid = $app->getUserState('resumeshare_companyid', 0);
         if(!$companyid) {
            echo '您需要先登录';
            exit;
         }
      } else {
         if (PFactory::isJobseekerUser()) {
            echo '您不是企业用户';
            exit();
         } else {
            $user = PFactory::getMasterUser();
            $companyid = $user->id;
         }
      }
      $template='part.resume.joblist';
      $jobid = JRequest::getInt('jobid', 0);
      $jobcity = JRequest::getString('jc', '');
      $jobikey = JRequest::getString('ik', '');
      $keyword = JRequest::getString('q', '');
      $searchUrl = JRequest::getString('searchurl', OC_Helper::linkToRoute('resumes_search'));
      
      if (!empty($jobid)) {
         $jobdata = PResumesHelper::getJob($jobid);
         $jobcity = $jobdata->id_city;
         if(empty($jobcity)) {
            $jobcity = 'other';
         }
         $jobikey = $jobdata->ikey;
         if(empty($jobikey)) {
            $jobikey = 'other';
         }
      }
      
      if(JRequest::getInt('alljobs')){
         $template='part.resume.alljoblist';
         $cityPageUrl = $ikeyPageUrl = OC_Helper::linkToRoute('resumes_joblist',array('alljobs'=>1));
      }else{
         $cityPageUrl = $ikeyPageUrl = OC_Helper::linkToRoute('resumes_joblist');
      }
      if (!empty($keyword)) {
         list($cityPageUrl, $selected) = PResumesHelper::getSearchItemHref($cityPageUrl, 'q', $keyword);
      }
      if (!empty($jobcity)) {
         list($cityPageUrl, $selected) = PResumesHelper::getSearchItemHref($cityPageUrl, 'jc', $jobcity);
      }
      if (!empty($jobikey)) {
         list($ikeyPageUrl, $selected) = PResumesHelper::getSearchItemHref($cityPageUrl, 'ik', $jobikey);
      } else {
         list($ikeyPageUrl, $selected) = PResumesHelper::getSearchItemHref($cityPageUrl, 'ik', 'all');
      }
      if(PCompanyHelper::isJobManager() && !PFactory::isMasterUser()) {
         $jobcitylist = PResumesHelper::getJobCityList($companyid,true,OC_User::getUser());
         $jobikeylist = PResumesHelper::getJobIkeyList($companyid,true,OC_User::getUser());
      }else{
         $jobcitylist = PResumesHelper::getJobCityList($companyid);
         $jobikeylist = PResumesHelper::getJobIkeyList($companyid);
      }
      $jobList = PResumesHelper::getJobList($companyid, 0, $keyword, $jobcity, $jobikey);
      $tmpl = new OCP\Template('resumes', $template, '');
      $tmpl->assign('cityPageUrl', $cityPageUrl);
      $tmpl->assign('ikeyPageUrl', $ikeyPageUrl);
      $tmpl->assign('searchUrl', $searchUrl);
      $tmpl->assign('jobcitylist', $jobcitylist);
      $tmpl->assign('jobikeylist', $jobikeylist);
      $tmpl->assign('jobList', $jobList);
      $tmpl->printPage();
   }
   
   public static function gettaglist() {
      $companyid = 0;
      if(!OC_User::isLoggedIn()) {
         $app = PFactory::getApplication();
         $companyid = $app->getUserState('resumeshare_companyid', 0);
         if(!$companyid) {
            echo '您需要先登录';
            exit;
         }
      } else {
         if (PFactory::isJobseekerUser()) {
            echo '您不是企业用户';
            exit();
         } else {
            $user = PFactory::getMasterUser();
            $companyid = $user->id;
         }
      }
   
      $tag = JRequest::getString('tag', '');
      $tagikey = JRequest::getString('ik', '');
      $keyword = JRequest::getString('q', '');
      $searchUrl = JRequest::getString('searchurl', OC_Helper::linkToRoute('resumes_search'));
   
      if (!empty($tag)) {
         $tagdata = PResumesHelper::getTag($tag);
         $tagikey = $tagdata->ikey;
         if(empty($tagikey)) {
            $tagikey = 'other';
         }
      }
   
      $ikeyPageUrl = OC_Helper::linkToRoute('resumes_taglist');
   
      if (!empty($keyword)) {
         list($ikeyPageUrl, $selected) = PResumesHelper::getSearchItemHref($ikeyPageUrl, 'q', $keyword);
      }
      if (!empty($tagikey)) {
         list($ikeyPageUrl, $selected) = PResumesHelper::getSearchItemHref($ikeyPageUrl, 'ik', $tagikey);
      } else {
         list($ikeyPageUrl, $selected) = PResumesHelper::getSearchItemHref($ikeyPageUrl, 'ik', 'all');
      }
   
      $tagikeylist = PResumesHelper::getTagIkeyList($companyid);
   
      $tagList = PResumesHelper::getTagList($companyid, 0, $keyword, $tagikey);
   
      $tmpl = new OCP\Template('resumes', 'part.resume.taglist', '');
      $tmpl->assign('ikeyPageUrl', $ikeyPageUrl);
      $tmpl->assign('searchUrl', $searchUrl);
      $tmpl->assign('tagikeylist', $tagikeylist);
      $tmpl->assign('tagList', $tagList);
      $tmpl->printPage();
   }
}
